﻿using System;
using System.Web;
using System.Configuration;
using System.Reflection;
using System.Web.Mvc;

namespace WebManager
{
    using Autofac;
    using Autofac.Integration.Mvc;
    using YK.Infrastructure.NH;
    using YK.Component.Data;
    using YK.Repository;
    using YK.Service.Account;
    

    public static class Bootstrapper
    {
        public static void Initialise()
        {
            

            // 获取连接字符串
            // just for test 
            ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["NHSample"];

            string providerName = connectionStringSettings.ProviderName;
            string connectionString = connectionStringSettings.ConnectionString;

            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.Register(c => new DatabaseFactory(providerName, connectionString)).As<IDatabaseFactory>().InstancePerLifetimeScope();
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();

            #region User
            builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerLifetimeScope();
            builder.RegisterType<UserService>().As<IUserService>().InstancePerLifetimeScope();
            #endregion

            #region Role
            builder.RegisterType<RoleRepository>().As<IRoleRepository>().InstancePerLifetimeScope();
            builder.RegisterType<RoleService>().As<IRoleService>().InstancePerLifetimeScope();
            #endregion


            IContainer container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}